﻿using System;

namespace Transformable_Engine_v2.Engine.Animation.Trajectory
{
    public class OvershootEasing : BaseTrajectory
    {
        private readonly Easing _easing;
        private double _overshoot;

        public OvershootEasing(Easing easing, double overshootFactor = 1.70158)
        {
            _easing = easing;
            _overshoot = overshootFactor;
        }


        public override double GetPercentageProgression(double elapsedDurationTime)
        {
            switch (_easing)
            {
                case Easing.EaseIn:
                    return GetEaseIn(elapsedDurationTime);
                case Easing.EaseOut:
                    return GetEaseOut(elapsedDurationTime);
                case Easing.EaseInAndOut:
                    return GetEaseInAndOut(elapsedDurationTime);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private double GetEaseOut(double elapsedDurationTime)
        {
            return Math.Pow(elapsedDurationTime - 1, 2) * ((_overshoot + 1) * (elapsedDurationTime - 1) + _overshoot) + 1;
        }

        private double GetEaseIn(double elapsedDurationTime)
        {
            return Math.Pow(elapsedDurationTime, 2) * ((_overshoot + 1) * elapsedDurationTime - _overshoot);
        }

        private double GetEaseInAndOut(double elapsedDurationTime)
        {
            if (elapsedDurationTime < 0.5)
                return 0.5*(Math.Pow(2 * elapsedDurationTime, 2)*(((_overshoot * (1.525))+1)*(2*elapsedDurationTime) - (_overshoot * 1.525)));

            return 0.5 * (Math.Pow(2 * elapsedDurationTime - 2, 2) * (((_overshoot * (1.525)) + 1) * (2 * elapsedDurationTime - 2) + (_overshoot * (1.525))) + 2);

        }
    }
} 